<?php
use lib\dao\Dao;

class DaoEvento extends Dao{
	
	public function categorias($categoria_id){
		$table = 'evento';
		
		$sql = "SELECT * FROM $table where categoria_id = '$categoria_id'";
		
		//Representa resultado obtenido de acceder a base de datos.
		$Dar = $this->DataAccess->retrieve($sql);
		return $Dar->fetch();	
	}

	public function lugares($lugar_id){
		$table = 'lugar';
		
		$sql = "SELECT * FROM $table where lugar_id = '$lugar_id'";
		
		//Representa resultado obtenido de acceder a base de datos.
		$Dar = $this->DataAccess->retrieve($sql);
		return $Dar->fetch();	
	}

	public function sectores($sector_id){
		$table = 'sector';
		
		$sql = "SELECT * FROM $table where sector_id = '$sector_id'";
		
		//Representa resultado obtenido de acceder a base de datos.
		$Dar = $this->DataAccess->retrieve($sql);
		return $Dar->fetch();
	}

	public function entradasXEvento(\lib\model\Model $M, \lib\dao\query\Query $Q){

		$table = $this->getTable($M);
		$query = $Q->prepare($M)->query;

		$sql   = " SELECT sum(cantidad) as entradas FROM compra c
					join ticket t on(c.ticket_id=t.id)
					$query
					GROUP BY evento_id";

		//esto tambien lo puedo sacar de result
		$binds = $Q->binds;
		$Dar   = $this->DataAccess->retrieve($sql, $binds);
		return $Dar->fetchAll();
	}

	public function detalleXSector(\lib\model\Model $M, \lib\dao\query\Query $Q){

		$table = $this->getTable($M);
		$query = $Q->prepare($M)->query;

		$sql   = "SELECT t.id,s.nombre,s.capacidad,if(sum(cantidad) is null,0,sum(cantidad)) as vendidas,t.precio 
				,if((s.capacidad-sum(cantidad) is null),s.capacidad,(s.capacidad-sum(cantidad))) as disponibles
                                ,if(100-ROUND((sum(cantidad)*100/s.capacidad)) is null,100,100-ROUND((sum(cantidad)*100/s.capacidad))) as porcentaje
				FROM sector s
				join ticket t on(t.sector_id=s.id)
				left join compra c on(t.id=c.ticket_id)
				$query
				GROUP BY sector_id ";

		$binds = $Q->binds;
		$Dar   = $this->DataAccess->retrieve($sql, $binds);
		return $Dar->fetchAll();
	}

	public function detalleEvento(\lib\model\Model $M, \lib\dao\query\Query $Q){


		$table = $this->getTable($M);
		$query = $Q->prepare($M)->query;

		$sql   = "SELECT e.id,e.nombre,e.imagen,c.nombre as categoria,e.descripcion,e.fecha,e.hora,e.token,l.nombre as lugar,e.imagen, datediff(e.fecha,now()) as dias
                         FROM $table e
				join lugar l on(e.lugar_id=l.id)
				join categoria c on(e.categoria_id=c.id)
				$query ";
		$binds = $Q->binds;
		$Dar   = $this->DataAccess->retrieve($sql, $binds);
		return $Dar->fetchAll();
	}
        
        public function eventoActivo(\lib\model\Model $M, \lib\dao\query\Query $Q){
		$table = $this->getTable($M);
		$query = $Q->prepare($M)->query;

		$sql   = " SELECT e.*,l.nombre as lugar FROM $table e
                            join lugar l on(e.lugar_id=l.id)
                            $query AND concat(fecha,' ',hora) >= now()";
                
		$binds = $Q->binds;
		$Dar   = $this->DataAccess->retrieve($sql, $binds);
		return $Dar->fetchAll();
	}
}